<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="90px">
      <!-- <el-form-item label="政策名称" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item> -->
      <el-form-item label="地区" prop="provinceId">
        <matic-select v-model:value="queryParams.provinceId" :objMap="{ value: 'id', label: 'name' }"
          :url="`area/getSonArea/forManager?parentId=${1}`"></matic-select> </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <page-table :url="url.list" ref="table" :tableForm="queryParams" :columns="columns">
      <template #action="scope">
        <!-- <el-tooltip content="修改" placement="top">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
        </el-tooltip> -->
        <el-tooltip content="设置结束时间" placement="top">
          <el-button link type="primary" icon="Clock" @click="handleUpdateEnd(scope.row)"></el-button>
        </el-tooltip>
      </template>
      <template #status="scope">
        <el-switch v-model="scope.row.status" v-if="scope.row.status != undefined" :active-value="1" :inactive-value="0"
          @change="handleDelete(scope.row, scope.row.status)"></el-switch>
      </template>
    </page-table>

    <!-- 添加或修改政策配置对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="roleRef" :model="form" :rules="rules" label-width="160px">
          <el-form-item v-show="title != '修改政策结束时间'" label="政策名称" prop="title">
            <el-input v-model="form.title" placeholder="请输入" />
          </el-form-item>
          <el-form-item v-show="title != '修改政策结束时间'" label="地区" prop="provinceId">
            <matic-select v-model:value="form.provinceId" :objMap="{ value: 'id', label: 'name' }"
              :url="`area/getSonArea/forManager?parentId=${1}`"></matic-select>
          </el-form-item>
          <el-form-item v-show="title != '修改政策结束时间'" label="容量电费单价(元)" prop="capacityPriceYuan" :rules="[{ required: title != '修改政策结束时间', message: '容量电费单价', trigger: 'blur' }]">
            <el-input v-model="form.capacityPriceYuan" placeholder="请输入" />
          </el-form-item>
        <el-form-item v-show="title != '修改政策结束时间'" label="容量溢出单价(元)" prop="capacityOverflowPriceYuan" :rules="[{ required: title != '修改政策结束时间', message: '容量溢出单价', trigger: 'blur' }]">
          <el-input v-model="form.capacityOverflowPriceYuan" placeholder="请输入" />
        </el-form-item>
        <el-form-item v-show="title != '修改政策结束时间'" label="容量溢出指标" prop="capacityOverflowQuota" :rules="[{ required: title != '修改政策结束时间', message: '容量溢出指标', trigger: 'blur' }]">
          <el-input v-model="form.capacityOverflowQuota" placeholder="请输入" />
        </el-form-item>
        <el-form-item v-show="title != '修改政策结束时间'" label="需量电费单价(元)" prop="demandPriceYuan" :rules="[{ required: title != '修改政策结束时间', message: '需量电费单价', trigger: 'blur' }]">
          <el-input v-model="form.demandPriceYuan" placeholder="请输入" />
        </el-form-item>
        <el-form-item v-show="title != '修改政策结束时间'" label="需量溢出单价(元)" prop="demandOverflowPriceYuan" :rules="[{ required: title != '修改政策结束时间', message: '需量溢出单价', trigger: 'blur' }]">
          <el-input v-model="form.demandOverflowPriceYuan" placeholder="请输入" />
        </el-form-item>
        <el-form-item v-show="title != '修改政策结束时间'" label="需量溢出指标" prop="demandOverflowQuota" :rules="[{ required: title != '修改政策结束时间', message: '需量溢出指标', trigger: 'blur' }]">
          <el-input v-model="form.demandOverflowQuota" placeholder="请输入" />
        </el-form-item>
        <el-form-item v-show="title != '修改政策结束时间'" label="功率因数指标" prop="glysQuota" :rules="[{ required: title != '修改政策结束时间', message: '功率因数指标', trigger: 'blur' }]">
          <el-input v-model="form.glysQuota" placeholder="请输入" />
        </el-form-item>
          <el-form-item v-show="title != '修改政策结束时间'" label="开始时间" prop="beginDatetime">
            <el-date-picker v-model="form.beginDatetime" style="width:100%" type="datetime" placeholder="请选择"
              format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
          </el-form-item>
          <el-form-item v-show="title == '修改政策结束时间'" label="结束时间" prop="endDatetime" :rules="[{ required: title == '修改政策结束时间', message: '结束时间不能为空', trigger: 'blur' }]">
            <el-date-picker v-model="form.endDatetime" style="width:100%" type="datetime" placeholder="请选择"
            format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
          </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Role">
import request from '@/utils/request'
const router = useRouter();
const { proxy } = getCurrentInstance();

const url = ref({
  list: 'policy/getPage/forManager',
  fine: 'policy/getOne/forManager',
  delete: 'policy/disableOrUse/forManager',
  add: 'policy/save/forManager',
  edit: 'policy/saveEndDatetime/forManager'
});


const open = ref(false);
const title = ref("");
const table = ref(null);

const columns = ref([{
  key: 'title',
  title: '政策名称',
  ellipsis: true,
  tooltip: true,
  align: "center"
},
{
  key: 'province',
  title: '地区',
  ellipsis: true,
  tooltip: true,
  align: "center"
},
{
  key: 'capacityPriceYuan',
  title: '容量电费单价(元)',
  ellipsis: true,
  tooltip: true,
  align: "center"
},{
    key: 'capacityOverflowPriceYuan',
    title: '容量溢出单价(元)',
    ellipsis: true,
    tooltip: true,
    align: "center"
  },{
    key: 'capacityOverflowQuota',
    title: '容量溢出指标',
    ellipsis: true,
    tooltip: true,
    align: "center"
  },{
    key: 'demandPriceYuan',
    title: '需量电费单价(元)',
    ellipsis: true,
    tooltip: true,
    align: "center"
  },{
    key: 'demandOverflowPriceYuan',
    title: '需量溢出单价(元)',
    ellipsis: true,
    tooltip: true,
    align: "center"
  },{
    key: 'demandOverflowQuota',
    title: '需量溢出指标',
    ellipsis: true,
    tooltip: true,
    align: "center"
  },{
    key: 'glysQuota',
    title: '功率因数指标',
    ellipsis: true,
    tooltip: true,
    align: "center"
  },
{
  key: 'beginDatetime',
  title: '开始时间',
  ellipsis: true,
  tooltip: true,
  align: "center"
},
{
  key: 'endDatetime',
  title: '结束时间',
  ellipsis: true,
  tooltip: true,
  align: "center"
},
{
  key: 'status',
  title: '停用|使用',
  align: 'center',
  width: 80,
  slots: {
    customKey: 'status',
  },
},
{
  title: '操作',
  key: 'action',
  slots: {
    customKey: 'action',
  },
  align: 'center',
  fixed: 'right',
  width: 80
}]);

/** 数据范围选项*/
const dataScopeOptions = ref([
  { value: "1", label: "全部数据权限" },
  { value: "2", label: "自定数据权限" },
]);

const handleDelete = (row, status) => {
  const id = row.id
  let text = row.status == '1' ? "启用" : "停用";
  proxy.$modal.confirm('确认要"' + text + '""' + row.title + '"吗?').then(function () {
    return request.get(url.value.delete, { params: { id } });
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功");
  }).catch(function () {
    row.status = row.status === 0 ? 1 : 0;
  });
}

const data = reactive({
  form: {},
  queryParams: {},
  rules: {
    title: [{ required: true, message: "政策名称不能为空", trigger: "blur" }],
    provinceId: [{ required: true, message: "省不能为空", trigger: "blur" }],
    beginDatetime: [{ required: true, message: "开始时间不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询列表 */
function getList() {
  table.value.onload();
}
/** 搜索按钮操作 */
function handleQuery() {
  table.value.init();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {};
  proxy.resetForm("roleRef");
}
/** 添加政策 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加政策";
}


/** 修改政策 */
function handleUpdateEnd(row) {
  reset();
  const id = row.id
  request.get(url.value.fine, { params: { id } }).then(response => {
    form.value = response.result;
    open.value = true;
    title.value = "修改政策结束时间";
  });
}
/** 修改政策 */
function handleUpdate(row) {
  reset();
  const id = row.id
  request.get(url.value.fine, { params: { id } }).then(response => {
    form.value = response.result;
    open.value = true;
    title.value = "修改政策";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["roleRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        request.post(url.value.edit, {id: form.value.id, endDatetime: form.value.endDatetime}).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        request.post(url.value.add, form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}


</script>
